package com.zxq.ruoyi.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.zxq.ruoyi.asyn.AsynProductService;
import com.zxq.ruoyi.domain.IotPacket;
import com.zxq.ruoyi.domain.vo.IotProductVo;
import com.zxq.ruoyi.domain.vo.ProductVoInfo;
import com.zxq.ruoyi.enums.EProductState;
import com.zxq.ruoyi.service.impl.IotPacketServiceImpl;
import com.zxq.ruoyi.service.impl.IotProductServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zxq.ruoyi.domain.IotProduct;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 产品Controller
 * 
 * @author ruoyi
 * @date 2022-09-14
 */
@RestController
@RequestMapping("/product")
public class IotProductController extends BaseController
{
    /**
     * 产品服务
     */
    @Autowired
    private IotProductServiceImpl iotProductService;

    /**
     * 数据包服务
     */
    @Autowired
    private IotPacketServiceImpl packetService;

    /**
     * 异步产品服务
     */
    @Autowired
    AsynProductService asynProductService;

    /**
     * 查询产品列表
     */
    @RequiresPermissions("iot:product:list")
    @GetMapping("/list")
    public ProductVoInfo list(IotProduct iotProduct)
    {
        startPage();
        List<IotProduct> list = iotProductService.selectIotProductList(iotProduct);
        List<IotProductVo> iotProductVos = new ArrayList<>();
        for (IotProduct item : list) {
           IotProductVo iotProductVo = new IotProductVo();
           BeanUtils.copyProperties(item,iotProductVo);
           EProductState eProductState = EProductState.getType(item.getState().intValue());
           switch (eProductState){
               case prepare:{
                   iotProductVo.setNextAction("运行");
               }
               break;
               case run: {
                   iotProductVo.setNextAction("停止");
               }
               break;
               case stop:{
                   iotProductVo.setNextAction("就绪");
               }
               break;
           }
            iotProductVos.add(iotProductVo);
        }

        ProductVoInfo rspData = new ProductVoInfo();
        rspData.setCode(200);
        rspData.setRows(iotProductVos);
        rspData.setMsg("查询成功");
        rspData.setTotal((new PageInfo(list)).getTotal());
        List<IotPacket> packets = packetService.selectIotPacketList(new IotPacket());
        rspData.setPacketList(packets);

        return rspData;
    }

    /**
     * 导出产品列表
     */
    @RequiresPermissions("iot:product:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotProduct iotProduct)
    {
        List<IotProduct> list = iotProductService.selectIotProductList(iotProduct);
        ExcelUtil<IotProduct> util = new ExcelUtil<IotProduct>(IotProduct.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @RequiresPermissions("iot:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(iotProductService.selectIotProductById(id));
    }

    /**
     * 新增产品
     */
    @RequiresPermissions("iot:product:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotProduct iotProduct)
    {
        return toAjax(iotProductService.insertIotProduct(iotProduct));
    }

    /**
     * 修改产品
     */
    @RequiresPermissions("iot:product:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotProduct iotProduct)
    {
        return toAjax(iotProductService.updateIotProduct(iotProduct));
    }

    /**
     * 删除产品
     */
    @RequiresPermissions("iot:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        asynProductService.delete(ids);
        return toAjax(iotProductService.deleteIotProductByIds(ids));
    }










    @PostMapping("startOver")
    public AjaxResult startProductServer(@RequestBody IotProductVo iotProductVo) {

        Long productId = iotProductVo.getId();
        asynProductService.changeState(productId);
        IotProduct iotProduct = iotProductService.selectIotProductById(iotProductVo.getId());
        BeanUtils.copyProperties(iotProduct,iotProductVo);
        EProductState eProductState = EProductState.getType(iotProduct.getState().intValue());
        switch (eProductState){
            case prepare:{
                iotProductVo.setNextAction("运行");
            }
            break;
            case run: {
                iotProductVo.setNextAction("停止");
            }
            break;
            case stop:{
                iotProductVo.setNextAction("就绪");
            }
            break;
        }
        return AjaxResult.success(iotProductVo);
    }










}
